// Copyright (c) 2007 Shufeng Tan

BROWSER	= {
  size: function (wh){
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			return wh=='w'?window.innerWidth:window.innerHeight;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			return wh=='w'? document.body.offsetWidth:document.body.offsetHeight;
	    	}
	}
	return wh=='w'?630:460;
  },
  open: function (url, title, w, h) {
        var top=Math.floor((BROWSER.size('h')-h)/2);
        var left=Math.floor((BROWSER.size('w')-w)/2);
        var attr='toolbar=0,scrollbars=1,status=0,fullscreen=0,titles=1,resizable=1,z-lock=1';
        window.open(url, title, STR.prf('width=%s,height=%s,top=%s,left=%s', w, h, top, left));
  }
}

TIME	= {
  now: function () {
	var t = new Date();
	return(t.getTime());
  },
  cookie: function (days){
	var d = new Date();
	d.setTime(d.getTime()+days*24*3600000);
	return(d.toGMTString());
  }
}

STR	= {
  prf: function (){
	var str = arguments[0];
	var screwy_re = /%[sd]/;
	for(var i = 1; i<arguments.length; i++){
		if (screwy_re.test(arguments[i])) {
			alert('Screwy argument '+i+' for sprintf: '+arguments[i]);
			return;
		}
	}
	var subst_re = /(.*?)(%[sd])(.*)/;
	var dol = [], nm = 0;
	while (dol = subst_re.exec(str)) {
		nm++;
		str = dol[1]+(dol[2]=='%s' ? arguments[nm] : Math.round(arguments[nm]))+dol[3];
	}
	return str;
  },

  join: function (){
	var joint = arguments[0];
	var str = arguments[1];
	for(var i = 2; i<arguments.length; i++){
		str += joint +""+ arguments[i];
	}
	return str;
  }
}

DYN	= {
  Debug: false,
  ih: function (id, html){
	if (document.getElementById) {
		if (DYN.Debug && document.getElementById(id) == undefined) {
			alert("DYN.ih error: id "+id+" not found. html=\n"+html);
		}
		var el = document.getElementById(id);
		if (el) {
			el.innerHTML=html;
		} else {
			alert("Error: Id "+id+" does not exist:\n"+html);
		}
	}else{
		with (document.layers[id].document){
			open();
			write(html);
			close();
		}
	}
  },
  xml_value: function (obj, tag) {
	var t;
	try {
		t = obj.getElementsByTagName(tag);
	} catch(e) {
		return("");
	}
	//if (t.length > 0 && t[0].firstChild) return t[0].lastChild.nodeValue;
	if (t.length > 0 && t[0].childNodes) {
		var val = "";
		var children = t[0].childNodes;
		for (var i = 0; i < children.length; i++) {
			val += children[i].nodeValue;
		}
		return val;
	}
	return("");
  },
  get_attr: function (obj, attr, re, dfault) {
	var v = obj.getAttribute(attr);
	if (v != undefined && re && re.test(v)) {
		return RegExp.$1;
	} else {
		return dfault;
	}
  }
}

ASYNC	= {
  xml: function (mesg_tag){
	var xmlreq = false;
	if(window.XMLHttpRequest){
		xmlreq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) { DYN.ih(mesg_tag, 'Failed to create XML request.') }
		}
	}
	return xmlreq;
  },
  handler: function (req, resp_handler, mesg_tag) {
	return function(){
		if (req.readyState == 4) {
			if (req.status == 200) {
				return resp_handler(req.responseXML);
			} else {
				DYN.ih(mesg_tag, 'Error handling XML response: '+req.statusText);
			}
		}
	}
  },
  get: function (url, handler, mesg_tag) {
	var req=ASYNC.xml(mesg_tag);
	req.onreadystatechange=ASYNC.handler(req, handler, mesg_tag);
	req.open('GET', url, true);
	req.send('');
  },
  post: function (url, params, handler, mesg_tag) {
	var req = ASYNC.xml(mesg_tag);
	req.onreadystatechange=ASYNC.handler(req, handler, mesg_tag);
	req.open('POST', url, true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.setRequestHeader("Content-Length", params.length);
	req.send(params);
  }
}

RANDOM	= {
  id: function (prefix) {
	return prefix+""+Math.floor(0xffffff*Math.random());
  },
  integer: function (min, max) {
	return min -0 + Math.floor(Math.random()*(max - min + 1));
  },
  decimal: function (min, max) {
	return min -0 + Math.random()*(max - min);
  },
  color: function (rmin,gmin,bmin,rmax,gmax,bmax) {
	var color="rgb(";
	var lo=new Array(rmin,gmin,bmin);
	var hi=new Array(rmax,gmax,bmax);
	for(var i=0; i<3; i++){
		color += RANDOM.integer(lo[i], hi[i]);
		if (i<2) color += ",";
	}
	return color+")";
  }
}
